package com.nanyang.tms.manager.waybill.shipment.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.date.DateUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.waybill.WaybillPayFeeDao;
import com.nanyang.tms.domain.cost.context.CalculateCostFreightContext;
import com.nanyang.tms.domain.waybill.WaybillPayFeeDO;
import com.nanyang.tms.domain.waybill.context.SaveTackInfoContext;
import com.nanyang.tms.domain.waybill.shipment.context.SaveWaybillShipmentContext;

@Component
public class SaveExpendHandler  extends AbstractTransHandler {
    @Autowired
	private WaybillPayFeeDao  waybillPayFeeDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		SaveWaybillShipmentContext saveWaybillShipmentContext = (SaveWaybillShipmentContext) context.getInnerContext(SaveWaybillShipmentContext.class);
		CalculateCostFreightContext calculateCostFreightContext = (CalculateCostFreightContext) context.getInnerContext(CalculateCostFreightContext.class);
		try {
			waybillPayFeeDao.insert(buildWaybillPayFee(saveWaybillShipmentContext, calculateCostFreightContext));
			
			
			SaveTackInfoContext saveTackInfoContext = (SaveTackInfoContext) context
					.getInnerContext(SaveTackInfoContext.class);
			
			BeanUtils.copyProperties(saveWaybillShipmentContext, saveTackInfoContext);
			Date date=new Date();
			saveTackInfoContext.setContent(String.format("包裹已经完成出货，出货时间：%s，出货地点：%ss",DateUtils.getTimeStampStr(date) ,saveWaybillShipmentContext.getAddress()));
			saveTackInfoContext.setTrackTime(date);
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}
	
	private WaybillPayFeeDO buildWaybillPayFee(SaveWaybillShipmentContext saveWaybillShipmentContext,CalculateCostFreightContext calculateCostFreightContext){
		String recordSeq = TmsSerializeConstant.WAYBILL_EXPENDE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.WAYBILL_INCOME_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
		WaybillPayFeeDO build = WaybillPayFeeDO.builder()
		.payCode(recordSeq)
		.waybillCode(saveWaybillShipmentContext.getWaybillCode())
		.companyCode(saveWaybillShipmentContext.getSupplierCode())
		.feeCode("FREIGHT")
		.feeName("运费")
		.predictAmount(calculateCostFreightContext.getAmount())
		.payable(calculateCostFreightContext.getAmount())
		.payment(calculateCostFreightContext.getAmount())
		.state(0)
		.build();
		build.setIp(saveWaybillShipmentContext.getIp());
		build.setGmtCreatePin(saveWaybillShipmentContext.getPin());
		build.setGmtModifiedPin(saveWaybillShipmentContext.getPin());
		return build;
	}

}
